package online.flowerinsnow.minecraft.bukkit.manhunt.runnable;

import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import online.flowerinsnow.minecraft.bukkit.manhunt.Manhunt;

public class DelayStartRunnable extends BukkitRunnable {
    private static BukkitTask instance;
    public static int timer = -1;
    @Override
    public void run() {
        timer--;
        if (timer < -1) timer = -1;
        setHunterStartInfo();
        if (timer == -1) {
            //noinspection ConstantConditions
            Manhunt.getTeamManager().getHunters().forEach(p -> p.teleport(Bukkit.getWorld("arena").getSpawnLocation()));
            GameTimerRunnable.start();
            stop();
        }
    }
    public synchronized static void start(int delay) {
        if (instance != null) stop();
        timer = delay;
        instance = new DelayStartRunnable().runTaskTimer(Manhunt.getInstance(), 0L, 20L);
    }
    public synchronized static void stop() {
        if (instance != null) {
            instance.cancel();
            instance = null;
        }
    }
    public static void setHunterStartInfo() {
        Bukkit.getOnlinePlayers().forEach(p -> Manhunt.getScoreboardManager().getPlayerScoreboard(p).setHunterStartInfo());
    }
}
